package com.ewhale.feitengguest.ui.shop.mine

import android.os.Bundle
import com.ewhale.feitengguest.R
import com.ewhale.feitengguest.dto.LoginInfoDto
import com.ewhale.feitengguest.dto.ShopAuthDto
import com.ewhale.feitengguest.presenter.shop.mine.SMinePresenter
import com.ewhale.feitengguest.receiver.TagAliasOperatorHelper.*
import com.ewhale.feitengguest.ui.auth.LoginActivity
import com.ewhale.feitengguest.ui.user.MainActivity
import com.ewhale.feitengguest.ui.user.mine.SettingActivity
import com.ewhale.feitengguest.utils.Contants
import com.ewhale.feitengguest.view.shop.mine.SMineView
import com.orhanobut.hawk.Hawk
import com.simga.library.activity.MBaseFragment
import com.simga.library.utils.HawkKey
import com.simga.library.utils.glide.GlideUtil
import kotlinx.android.synthetic.main.fragment_shop_mine.*

class SMineFragment : MBaseFragment<SMinePresenter>(), SMineView {

    override fun getViewId(): Int {
        return R.layout.fragment_shop_mine
    }

    override fun init(savedInstanceState: Bundle?) {
    }
    override fun initListener() {
        super.initListener()
        tv_setting.setOnClickListener { SettingActivity.open(mContext) }
        ll_user_info.setOnClickListener {ShopUserInfoActivity.open(mContext) }
        rl_auth.setOnClickListener {ShopAuthActivity.open(mContext) }
        rl_mywallet.setOnClickListener { ShopWalletActivity.open(mContext)}
        rl_xuanshang.setOnClickListener {
            if (!Hawk.get(HawkKey.IS_USER_LOGIN, false)) {
                LoginActivity.open(mContext)
            } else {
                Hawk.put(HawkKey.NOW_APP_STATUS, Contants.USER_STATUS)
                //重新设置极光推送
                val tagAliasBean = TagAliasBean()
                tagAliasBean.action = ACTION_SET
                sequence++
                tagAliasBean.alias = Hawk.get(HawkKey.USER_AUTHENTICATION)
                tagAliasBean.isAliasAction = true
                getInstance().handleAction(context, sequence, tagAliasBean)
                MainActivity.open(mContext)
                finish()
//                showLoading()
//                AuthHelper.getInstance().login(mContext, Hawk.get(HawkKey.USER_HX_ID,""),  Hawk.get(HawkKey.USER_HX_PASSWORD,""), object : AuthHelper.AuthCallback {
//                    override fun onSuccess() {
//                        dimissProLoading()
//                        Hawk.put(HawkKey.NOW_APP_STATUS, Contants.USER_STATUS)
//                        //删除极光
//                        val tagAliasBean = TagAliasOperatorHelper.TagAliasBean()
//                        tagAliasBean.action = ACTION_DELETE
//                        sequence++
//                        tagAliasBean.alias = ""
//                        tagAliasBean.isAliasAction = true
//                        TagAliasOperatorHelper.getInstance().handleAction(context, sequence, tagAliasBean)
//                        MainActivity.open(mContext)
//                        finish()
//                    }
//                    override fun onError(errorCode: Int, errorStr: String) {
//                        dismissLoading()
//                        showToast("errorCode : $errorCode ! errorStr : $errorStr")
//                    }
//                })
            }
        }
    }

    override fun onResume() {
        super.onResume()
        val dto = Hawk.get<LoginInfoDto>(HawkKey.SHOP_LOGIN_INFO, null)
        if (dto != null) {
            GlideUtil.loadPicture(dto.headPhoto, iv_head, R.drawable.default_image)
            tv_phone.text = if(dto.shopType == 1)"个人" else "公司"
            tv_name.text = dto.nickName
            if(dto.authStatus == 3){
                tv_authStatus.text = "已认证"
            }else{
                tv_authStatus.text = "未认证"
            }
        }
        presenter!!.getAuthInfo()
    }
    override fun showAuthInfo(shopAuth: ShopAuthDto?) {
        if(shopAuth!!.status == 3){
            tv_authStatus.text = "已认证"
        }else{
            tv_authStatus.text = "未认证"
        }
    }
    override fun showProLoading() {
        showLoading()
    }
    override fun dimissProLoading() {
        dismissLoading()
    }
    override fun showErrorMessage(postCode: Int, code: String?, message: String?) {
        if (postCode == 3 && code == "null") {//当返回的是null的时候，就是未认证
            tv_authStatus.text = "未认证"
        } else {
            showErrorMsg(code, message)
        }
    }
    override fun resetAvatar(data: String?) {}
    override fun resetName(data: String?) {}
}
